$NG.AllReady(function (
	page,
	{ useValuesChange, useClick, useUpdateRow, useUpdateRows, useDataIndexChange }
) {
	console.log("Data ready event triggered");
	var mstform = $NG.getCmpApi("p_form_svc_cost_budget");
	var dgrid = $NG.getCmpApi("p_form_svc_labor_budget");
	var dgrid1 = $NG.getCmpApi("p_form_svc_daliy_budget");

	{
		//初始化逻辑BEGIN
		if ($NG.getCmpApi("p_form_svc_cost_budget").getItem("phid_pc").getValue()) {
			getPtInfoByPhid();
			cntMFilter();
		}
	} //初始化逻辑END

	{
		//表头字段变化逻辑BEGIN
		useValuesChange(({ args }) => {
			getPtInfoByPhid();
			cntMFilter();
		}, "p_form_svc_cost_budget.phid_pc");
	} //表头字段变化逻辑END

	{
		//表体字段汇总逻辑BEGIN
		useUpdateRows(({ args }) => {
			const rows = args[0];
			caculatedgrid();
		}, "p_form_svc_labor_budget");

		useUpdateRows(({ args }) => {
			const rows = args[0];
			caculatedgrid();
		}, "p_form_svc_daliy_budget");
	} //表体字段汇总逻辑END

	//监听合同名称变化
	useValuesChange(({ args }) => {
		console.log(args[0].u_cnt.origin.bill_no, "argsqqqqqqqqqqqqq");
		$NG.updateState((updater) => {
			updater.data.p_form_svc_cost_budget.setProps({
				u_cnt_no: {
					label: args[0].u_cnt.origin.bill_no,
					value: args[0].u_cnt.origin.bill_no,
				},
			});
		});
	}, "p_form_svc_cost_budget.u_cnt");
	////////////////////////////////////////////////////////////////////////////////////
	function caculatedgrid() {
		console.log("caculatedgrid begin");
		let u_total_svc_labor_amt = 0;
		let u_total_daily_amt = 0;
		let u_total_svc_amt = 0;

		const Rows1 = $NG.getCmpApi("p_form_svc_labor_budget").getRows(); //服务人工页签
		const Rows2 = $NG.getCmpApi("p_form_svc_daliy_budget").getRows(); //日常费用页签
		Rows1.forEach((items) => {
			u_total_svc_labor_amt += parseFloat(items.u_amt) || 0;
		});
		Rows2.forEach((items) => {
			u_total_daily_amt += parseFloat(items.u_annual_svc_amt);
		});

		u_total_svc_amt = u_total_svc_labor_amt + u_total_daily_amt;

		$NG.updateState((updater) => {
			updater.data.p_form_svc_cost_budget.setProps({
				u_total_svc_labor_amt: u_total_svc_labor_amt, //合计服务人工费
				u_total_daily_amt: u_total_daily_amt, //合计日常费用
				u_total_svc_amt: u_total_svc_amt, //合计服务费总额
			});
		});
		console.log("u_total_svc_amt:" + u_total_svc_amt);
		console.log("caculatedgrid end");
	}
	function getPtInfoByPhid() {
		if (mstform.getItem("phid_pc").getValue()) {
			$NG.execServer(
				"getPtInfoByPhid",
				{ phid: mstform.getItem("phid_pc").getValue() },
				function (res) {
					const datas = JSON.parse(res.data)[0].extendObjects;
					console.log(datas);
					//帮助字段赋值
					$NG.updateState((updater) => {
						updater.data.p_form_svc_cost_budget.setProps({
							u_agreed_sts_dt: datas.start_date, //计划完工日期
							u_agreed_pcl_dt: datas.end_date, //计划竣工日期
							u_pc_no: datas.pc_no, //项目编码
							u_sqgw: {
								//售前顾问
								value: datas.user_sqgw_phid,
								label: datas.user_sqgw_name,
							},
							u_division: {
								//事业部or组织
								value: datas.phid_org_phid,
								label: datas.phid_org_name,
							},
							u_regional_center: {
								//区域中心
								value: datas.regional_center_phid,
								label: datas.regional_center_name,
							},
							u_product_line: {
								//产品线
								value: datas.midwife_phid,
								label: datas.midwife_name,
							},
						});
					});
				}
			);
		}
	}
	function cntMFilter() {
		if (!mstform.getItem("phid_pc").getValue()) {
			return;
		}
		$NG.updateUI((updater, state) => {
			updater.fieldSetForm.p_form_svc_cost_budget.u_cnt.setProps({
				onBeforeOpen: async () => {
					var phid_pc = $NG
						.getCmpApi("p_form_svc_cost_budget")
						.getItem("phid_pc")
						.getValue();
					if (!phid_pc) {
						$NG.alert("请先选择项目");
						return false;
					} else {
						return true;
					}
				},
				...(!!$NG
					.getCmpApi("p_form_svc_cost_budget")
					.getItem("phid_pc")
					.getValue() && {
					clientSqlFilter: {
						bill_type: 1,
						phid_pc: $NG
							.getCmpApi("p_form_svc_cost_budget")
							.getItem("phid_pc")
							.getValue(),
					},
				}),
			});
		});
	}
});
